//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package data_anno_dao

import (
	"context"
	"fmt"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/data_anno_task_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type MemberInfoDbItem struct {
	MemberId      string `bson:"_id"`
	AnnoProjectId string `bson:"annoProjectId"`
	ProjectId     string `bson:"projectId"`
	MemberUserId  string `bson:"memberUserId"`
	TaskCount     uint32 `bson:"taskCount"`
	DoneCount     uint32 `bson:"doneCount"`
	TimeStamp     int64  `bson:"timeStamp"`
}

func (item *MemberInfoDbItem) ToMemberInfo() *data_anno_task_api.MemberInfo {
	return &data_anno_task_api.MemberInfo{
		MemberUserId: item.MemberUserId,
		TaskCount:    item.TaskCount,
		DoneCount:    item.DoneCount,
	}
}

type _AnnoMemberDao struct {
	mc *mongo.Collection
}

func newAnnoMemberDao(db *mongo.Database) (*_AnnoMemberDao, error) {
	mc := db.Collection(db_ns.DB_NS_DATA_ANNO + "member")
	_, err := mc.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys: bson.D{{"annoProjectId", 1}, {"timeStamp", 1}},
		},
		{
			Keys: bson.D{{"projectId", 1}, {"memberUserId", 1}},
		},
		{
			Keys: bson.D{{"annoProjectId", 1}, {"memberUserId", 1}},
		},
	})
	if err != nil {
		return nil, err
	}
	return &_AnnoMemberDao{mc: mc}, nil
}

func (db *_AnnoMemberDao) Insert(ctx context.Context, dbItem *MemberInfoDbItem) error {
	dbItem.MemberId = db.genMemberId(dbItem.AnnoProjectId, dbItem.MemberUserId)
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_AnnoMemberDao) genMemberId(annoProjectId, memberUserId string) string {
	return fmt.Sprintf("%s:%s", annoProjectId, memberUserId)
}

func (db *_AnnoMemberDao) Remove(ctx context.Context, annoProjectId, memberUserId string) error {
	memberId := db.genMemberId(annoProjectId, memberUserId)
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": memberId, "annoProjectId": annoProjectId})
	return err
}

func (db *_AnnoMemberDao) RemoveByAnnoProject(ctx context.Context, annoProjectId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"annoProjectId": annoProjectId})
	return err
}

func (db *_AnnoMemberDao) Exist(ctx context.Context, annoProjectId, memberUserId string) (bool, error) {
	memberId := db.genMemberId(annoProjectId, memberUserId)
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": memberId, "annoProjectId": annoProjectId})
	return count > 0, err
}

func (db *_AnnoMemberDao) Get(ctx context.Context, annoProjectId, memberUserId string) (*MemberInfoDbItem, error) {
	memberId := db.genMemberId(annoProjectId, memberUserId)
	retItem := &MemberInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": memberId, "annoProjectId": annoProjectId}).Decode(retItem)
	if err != nil {
		return nil, err
	}
	return retItem, nil
}

func (db *_AnnoMemberDao) UpdateTaskCount(ctx context.Context, annoProjectId, memberUserId string, count uint32) error {
	memberId := db.genMemberId(annoProjectId, memberUserId)
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": memberId, "annoProjectId": annoProjectId}, bson.M{"$set": bson.M{"taskCount": count}})
	return err
}

func (db *_AnnoMemberDao) UpdateDoneCount(ctx context.Context, annoProjectId, memberUserId string, count uint32) error {
	memberId := db.genMemberId(annoProjectId, memberUserId)
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": memberId, "annoProjectId": annoProjectId}, bson.M{"$set": bson.M{"doneCount": count}})
	return err
}

func (db *_AnnoMemberDao) Count(ctx context.Context, annoProjectId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"annoProjectId": annoProjectId})
	return uint32(count), err
}

func (db *_AnnoMemberDao) List(ctx context.Context, annoProjectId string) ([]*MemberInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"annoProjectId": annoProjectId}, options.Find().SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*MemberInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_AnnoMemberDao) ListByMember(ctx context.Context, annoProjectIdList []string, memberUserId string) ([]*MemberInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"annoProjectId": bson.M{"$in": annoProjectIdList}, "memberUserId": memberUserId})
	if err != nil {
		return nil, err
	}
	retList := []*MemberInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_AnnoMemberDao) DistinctProjectByMember(ctx context.Context, projectId, memberUserId string) ([]string, error) {
	tmpList, err := db.mc.Distinct(ctx, "annoProjectId", bson.M{"projectId": projectId, "memberUserId": memberUserId})
	if err != nil {
		return nil, err
	}
	retList := []string{}
	for _, tmpItem := range tmpList {
		retList = append(retList, tmpItem.(string))
	}
	return retList, nil
}
